<?php
namespace app\index\model;

use think\Model;

class Common extends Model
{
    protected $_dbType = 'redis';
    protected $_host = '127.0.0.1';
    protected $_port = 6379;
    public $redis = '';

    /**
     * 初始化类
     * @param array $conf 数据库配置
     */
    public function __construct() {

        $this->_connect($this->_dbType);

    }
    /**
     * 连接数据库的方法
     */
    protected function _connect($type) {
				if ( $type == 'memcache') {
						$this->redis = new \Memcache;
				} else if($type == 'redis'){
						$this->redis = new \Redis;
				} else {
						return false;
				}
				$this->redis->connect($this->_host,$this->_port);
				return $this->redis;
    }
    /**
     * 查询数据
     *
     * @author Cheng
     *
     * @DateTime 2016-05-28
     *
     * @param    string    $tName  redis的key
     * @param    array    $userId    id数组
     *
     * @return   array            数据列表
     */
    public function select(  $tName, $userId = '')
    {
        $array = array();
        $list = $this->redis->keys($tName.':'.$tName.'_id:*');
        if ( empty($userId) ) {
            for( $i=1; $i <= count($list)/4;$i++){
                $flag = $this->redis->get($tName.':'.$tName.'_id:'.$i.':flag');
                if($flag === 0) {
                    continue;
                }
                $array[$i]['starttime'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':starttime');
                $array[$i]['content'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':content');
                $user_id = $this->redis->get($tName.':'.$tName.'_id:'.$i.':user_id');
                $array[$i]['user_id']= $this->redis->get($tName.':'.$tName.'_id:'.$i.':user_id');
                $array[$i]['username'] = $this->redis->get('user_tab:user_id:'.$user_id.':username');

                $array[$i]['sort'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':sort');
                $array[$i]['flag'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':flag');

            }
        } else {
            for( $i=0; $i <count($list)/4;$i++){
                $flag = $this->redis->get($tName.':'.$tName.'_id:'.$i.':flag');
                if($flag === 0) {
                    continue;
                }
                $user_id= $this->redis->get($tName.':'.$tName.'_id:'.$i.':user_id');
                if($userId == $user_id) {
                    $array[$i]['starttime'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':starttime');
                    $array[$i]['content'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':content');
                    $user_id = $this->redis->get($tName.':'.$tName.'_id:'.$i.':user_id');
                    $array[$i]['user_id']= $this->redis->get($tName.':'.$tName.'_id:'.$i.':user_id');
                    $array[$i]['username'] = $this->redis->get('user_tab:user_id:'.$user_id.':username');
                    $array[$i]['sort'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':sort');
                    $array[$i]['flag'] = $this->redis->get($tName.':'.$tName.'_id:'.$i.':flag');

                }
            }
        }
        return $array;
    }
    /**
     * 修改数据
     *
     * @author Cheng
     *
     * @DateTime 2016-05-28
     *
     * @param    string     $tName 表名
     * @param    主键       $id    键名
     * @param    array      $data  修改的数据
     *
     * @return   bool            成功与否
     */
    public function updata($tName , $id , $data)
    {
        foreach ($data as $key => $value) {
            $insterId = $this->redis->set( $tName.':'.$tName."_id:".$id.":".$key  , $value);
        }
        return $id;
    }
    /**
     * 删除
     *
     * @author Cheng
     *
     * @DateTime 2016-05-28
     *
     * @param    表名     $tName 表名
     * @param    主键     $id
     *
     * @return   [type]            [description]
     */
    public function del($tName , $id )
    {
        $bool = $this->redis->set($tName.':'.$tName."_id:".$primary.":flag" , 1);
        return $bool;
    }
    /**
     * 添加数据
     *
     * @author Cheng
     *
     * @DateTime 2016-05-28
     *
     * @param    表名     $tName 表名
     * @param    数据     $array 数据
     */
    public function inster($tName , $data)
    {
        $primary = $this->redis->incr('global:'.$tName.'_id');
        foreach ($data as $key => $value) {
            $insterId = $this->redis->set( $tName.':'.$tName."_id:".$primary.":".$key  , $value);
        }
        return $primary;
    }
}
